package com.cgy.utils.tokenizer.dictionary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * 自定义字段
 * @author Administrator
 *
 */
public class Dictionary {

	private ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();
	private ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>();
	/**
	 * 初始化
	 * @return
	 */
	public static Dictionary newDictionary() {
		return new Dictionary();
	}
	/**
	 * 
	 * @param k
	 * @param v
	 * @return
	 */
	public Dictionary addDictionary(String k) {
		concurrentLinkedQueue.add(k);
		return this;
	}
	/**
	 * 
	 * @param k
	 * @param v
	 * @return
	 */
	public Dictionary addDictionary(String k, String v) {
		concurrentHashMap.putIfAbsent(k, v);
		return this;
	}
	/**
	 * 
	 * @param k
	 * @param v
	 * @return
	 */
	public String getDictionary(String k) {
		return concurrentHashMap.get(k);
	}
	/**
	 * 
	 * @return
	 */
	public Map<String, String> toMap() {
		Map<String, String> maps = new HashMap<>();
		for (Map.Entry<String, String> entries : concurrentHashMap.entrySet()) {
			maps.put(entries.getKey(), entries.getValue());
		}
		return maps;
	}
	/**
	 * 
	 * @return
	 */
	public List<String> toList() {
		Iterator<String> iterator = concurrentLinkedQueue.iterator();
		List<String> result = new ArrayList<>(concurrentLinkedQueue.size());
		while(iterator.hasNext()) {
			result.add(iterator.next());
		}
		return result;
	}
	
	public boolean isEmpty() {
		return concurrentHashMap.isEmpty() | concurrentLinkedQueue.isEmpty();
	}
}
